package com.jiuyin.dianjing.ui.fragment.team;

import android.os.Bundle;
import android.widget.ImageView;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.adapter.BaseRecyclerAdapter;
import com.jiuyin.dianjing.adapter.SmartViewHolder;
import com.jiuyin.dianjing.api.base.DefaultApiResult;
import com.jiuyin.dianjing.api.enums.ApiEnum;
import com.jiuyin.dianjing.api.interfaces.ApiResultCallback;
import com.jiuyin.dianjing.api.server.ServerApi;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.model.TeamGameRecordsModel;
import com.jiuyin.dianjing.ui.fragment.base.BaseFragment;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TEAMID;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TEAM_ID;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;

/**
 * Description:
 * File: TeamGameRecordsFragment.java
 * Author: Administrator
 * Create: 2020/5/13 17:33
 */
public class TeamGameRecordsFragment extends BaseFragment {

    @BindView(R.id.rv_teams)
    RecyclerView rvTeams;

    private BaseRecyclerAdapter<TeamGameRecordsModel> mAdapter;
    private List<TeamGameRecordsModel> recordModels = new ArrayList<>();
    private String teamId;

    public static TeamGameRecordsFragment newInstance(String teamId) {
        TeamGameRecordsFragment fragment = new TeamGameRecordsFragment();
        Bundle args = new Bundle();
        args.putSerializable(KEY_TEAM_ID, teamId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void fetchData() {
        HashMap<String, Object> data = new HashMap<>();
        data.put(KEY_TOKEN, HelperApplication.getToken());
        data.put(KEY_TEAMID, teamId);
        JSONObject jsonObject = new JSONObject(data);
        ApiResultCallback requestCallback = new DefaultApiResult() {

            @Override
            public void onSuccess(JsonObject data) {
                if (data.has("teamDataList")) {
                    List<TeamGameRecordsModel> competeList = new Gson().fromJson(data.getAsJsonArray("teamDataList"), new TypeToken<List<TeamGameRecordsModel>>() {
                    }.getType());
                    onRefresh(competeList);
                }
            }
        };
        ServerApi.post(ApiEnum.TEAM_DETAIL, jsonObject, null, progress, d -> addDisposable(d), requestCallback);
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_team_game_records;
    }

    @Override
    protected void initData() {
        Bundle argumentsBundle = getArguments();
        if (null != argumentsBundle) {
            teamId = argumentsBundle.getString(KEY_TEAM_ID);
        }
    }

    @Override
    protected void initView() {
        mAdapter = new BaseRecyclerAdapter<TeamGameRecordsModel>(R.layout.item_team_game_records) {
            @Override
            protected void onBindViewHolder(SmartViewHolder holder, TeamGameRecordsModel model, int position) {
                ImageView ivHead = holder.itemView.findViewById(R.id.img_game);
                Glide.with(getActivity())
                        .load(model.logo)
                        .override(ivHead.getWidth(), ivHead.getHeight())
                        .into(ivHead);
                holder.text(R.id.tv_match_title, model.name);
                holder.text(R.id.tv_match_status1, model.compet_num);
                holder.text(R.id.tv_match_status3, model.rank_num);
            }
        };
        DividerItemDecoration divider = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        rvTeams.addItemDecoration(divider);
        rvTeams.setAdapter(mAdapter);
    }

    public void onRefresh(List<TeamGameRecordsModel> models) {
        mAdapter.refresh(models);
    }

}
